package com.dts.evaluate.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.dts.evaluate.R;
import com.dts.evaluate.bean.ProjectDataBean;
import com.dts.evaluate.common.Constants;
import com.dts.evaluate.ui.fragment.BasicInfoFragment;
import com.dts.evaluate.ui.fragment.BasisFragment;
import com.dts.evaluate.ui.fragment.ChargeFragment;
import com.dts.evaluate.ui.fragment.ConditionFragment;
import com.dts.evaluate.ui.fragment.ContactFragment;
import com.dts.evaluate.ui.fragment.MaterialFragment;
import com.dts.evaluate.ui.view.TitleView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 办事指南
 */
public class BsznActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener,TitleView.OnBackClikListener {

    @Bind(R.id.title_view)
    TitleView titleView;
    @Bind(R.id.radio_group)
    RadioGroup radioGroup;
    @Bind(R.id.basic_info_radio_button)
    RadioButton basicInfoRadioButton;
    @Bind(R.id.condition_radio_button)
    RadioButton conditionRadioButton;
    @Bind(R.id.material_radio_button)
    RadioButton materialRadioButton;
    @Bind(R.id.basis_radio_button)
    RadioButton basisRadioButton;
    @Bind(R.id.charge_radio_button)
    RadioButton chargeRadioButton;
    @Bind(R.id.contact_radio_button)
    RadioButton contactRadioButton;
    @Bind(R.id.fragment_container)
    FrameLayout fragmentContainer;

    private BasicInfoFragment fragment1;
    private ConditionFragment fragment2;
    private MaterialFragment fragment3;
    private BasisFragment fragment4;
    private ChargeFragment fragment5;
    private ContactFragment fragment6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_guide);
        ButterKnife.bind(this);

        ProjectDataBean.ApproveProjectBean approveProjectBean  = (ProjectDataBean.ApproveProjectBean) getIntent().getSerializableExtra(Constants.APPROVE_PROJECT_BEAN);

        initFragments(approveProjectBean);

        initView();

    }

    private void initView() {


       // getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,fragment1).commit();

        //设置radioGroup 监听
        radioGroup.setOnCheckedChangeListener(this);

        //设置titleView返回监听
        titleView.setOnBackClickListener(this);

        //默认选中基本信息
      // radioGroup.check(R.id.basic_info_radio_button);
        ((RadioButton)(radioGroup.getChildAt(0))).toggle();

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.basic_info_radio_button:
                showFragment(fragment1);
                break;
            case R.id.condition_radio_button:
                showFragment(fragment2);
                break;
            case R.id.material_radio_button:
                showFragment(fragment3);
                break;
            case R.id.basis_radio_button:
                showFragment(fragment4);
                break;
            case R.id.charge_radio_button:
                showFragment(fragment5);
                break;
            case R.id.contact_radio_button:
                showFragment(fragment6);
                break;
        }
    }

    /**
     * 初始化所有fragment
     */
    private void initFragments(ProjectDataBean.ApproveProjectBean approveProjectBean) {
        fragment1 = BasicInfoFragment.newInstance(approveProjectBean, null);
        fragment2 = ConditionFragment.newInstance(approveProjectBean, null);
        fragment3 = MaterialFragment.newInstance(approveProjectBean, null);
        fragment4 = BasisFragment.newInstance(approveProjectBean, null);
        fragment5 = ChargeFragment.newInstance(approveProjectBean, null);
        fragment6 = ContactFragment.newInstance(approveProjectBean, null);
    }

    /**
     * 控制fragment是添加 还是显示
     *
     * @param fragment
     */
    private void showFragment(Fragment fragment) {
        hideAllFragment();

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(R.id.fragment_container, fragment);
            transaction.show(fragment);
        }
        transaction.commit();
    }

    /**
     * 隐藏所有fragment
     */
    private void hideAllFragment() {
        hideFragment(fragment1);
        hideFragment(fragment2);
        hideFragment(fragment3);
        hideFragment(fragment4);
        hideFragment(fragment5);
        hideFragment(fragment6);
    }

    /**
     * 隐藏单个fragment
     *
     * @param fragment
     */
    private void hideFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fragment.isAdded() && fragment.isVisible()) {
            transaction.hide(fragment);
            transaction.commit();
        }
    }

    @Override
    public void onTitleViewClik(View v) {
        finish();
    }
}
